package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.enums.EquipmentEnums;
import com.mxpio.erp.common.equipment.entity.EquipmentCalibrationProgress;
import com.mxpio.erp.common.equipment.service.EquipmentCalibrationProgressService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "EquipmentCalibrationProgressController}", description = "设备检定-检定操作进度")
@RestController
@RequestMapping("/erp/equipment/calibration/progress/")
public class EquipmentCalibrationProgressController {

    @Autowired
    private EquipmentCalibrationProgressService progressService;

    @GetMapping("list")
    @Operation(summary = "设备检定-检定操作进度列表", description = "获取设备检定-检定操作进度列表（无分页）", method = "GET")
    public Result<List<EquipmentCalibrationProgress>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentCalibrationProgress> list = progressService.list(EquipmentCalibrationProgress.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "设备检定-检定操作进度列表", description = "获取设备检定-检定操作进度列表（分页）", method = "GET")
    public  Result<Page<EquipmentCalibrationProgress>> page(@Parameter(description = "查询参数") Criteria criteria,
                                                            @Parameter(description = "每页条数") Integer pageSize,
                                                            @Parameter(description = "页号") Integer pageNo)throws UnsupportedEncodingException{
        PageRequest page = PageRequest.of(pageNo - 1, pageSize);
        Page<EquipmentCalibrationProgress> pageRequest = progressService.listPage(EquipmentCalibrationProgress.class, page, criteria);
        return Result.OK(pageRequest);
    }

    @GetMapping("list/{calcode}")
    @Operation(summary = "根据calCode工单编码获取设备检定-检定操作进度列表", description = "根据bizNo工单编码获取设备检定-检定操作进度列表", method = "GET")
    public Result<List<EquipmentCalibrationProgress>> getById(@PathVariable(name = "calCode", required = true) String calCode){
        Criteria criteria = Criteria.create();
        criteria.addCriterion("calCode", Operator.EQ, calCode);
        List<EquipmentCalibrationProgress> list = progressService.list(EquipmentCalibrationProgress.class, criteria);
        return Result.OK(list);
    }

    @RequestMapping("add")
    @Operation(summary = "保存设备检定-检定操作进度", description = "保存设备检定-检定操作进度")
    public Result<EquipmentCalibrationProgress> add(@RequestBody EquipmentCalibrationProgress calibrationProgress){
        String executionStatus = calibrationProgress.getExecutionStatus();

        if (executionStatus.equals(EquipmentEnums.Calibration.COMPLETED.getCode())){ //当检定状态为 已完成时，不可上报
            return Result.error("当检定状态为已完成时，不可上报");
        }else {
            progressService.save(calibrationProgress);
            return Result.OK(calibrationProgress);
        }
    }

    @RequestMapping("save")
    @Operation(summary = "保存设备检定-检定操纵进度", description = "保存设备检定-检定操纵进度")
    public Result<List<EquipmentCalibrationProgress>> save(@RequestBody List<EquipmentCalibrationProgress> details){
        progressService.save(details);
        return Result.OK(details);
    }
}
